package cn.antho.system.service.impl;

import cn.antho.system.domain.Role;
import cn.antho.system.mapper.MenuMapper;
import cn.antho.system.mapper.PermissionMapper;
import cn.antho.system.service.IRoleService;
import cn.antho.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author antho
 * @since 2022-10-24
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional
    public void add(Role role) {
        super.add(role);
        // 添加菜单权限
        menuMapper.addMiddle(role.getId(), role.getMenus());
        // 添加按钮权限
        permissionMapper.addMiddle(role.getId(), role.getPermissions());
    }

    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        // 删除菜单权限
        menuMapper.delMiddle(role.getId());
        // 删除按钮权限
        permissionMapper.delMiddle(role.getId());
        // 添加菜单权限
        menuMapper.addMiddle(role.getId(), role.getMenus());
        // 添加按钮权限
        permissionMapper.addMiddle(role.getId(), role.getPermissions());

    }

    @Override
    @Transactional
    public void delete(Long id) {
        super.delete(id);
        // 删除菜单权限
        menuMapper.delMiddle(id);
        // 删除按钮权限
        permissionMapper.delMiddle(id);
    }
}
